//
//  LSRBaseController.m
//  BaiSiProject
//
//  Created by lshenrong on 16/7/21.
//  Copyright © 2016年 lshenrong. All rights reserved.
//

#import "LSRBaseController.h"

static NSString *ID=@"Cell";
@interface LSRBaseController ()<UICollectionViewDataSource,UICollectionViewDelegate>

//滚动的顶部条
@property(weak,nonatomic)UIScrollView *topView;

@property(weak,nonatomic)UICollectionView *collectionView;
/**<#name#>*/
@property(strong,nonatomic )NSMutableArray *buttonArray;

@property(weak,nonatomic)UIView *redView;

@property(weak,nonatomic)UIButton *selctedBtn;
@property(assign,nonatomic)BOOL isInitial;

@end

@implementation LSRBaseController

#pragma mark---lazyLoad
-(NSMutableArray *)buttonArray
{
    if (!_buttonArray) {
        
        _buttonArray=[NSMutableArray array];
    }
    
    
    return _buttonArray;
    
    
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    /*注意添加的顺序*/
    //添加底部collectionView
    [self setUpBottomView];
    //添加顶部scrollView
    [ self setUpTopView];
  
    
    //取消额外的滚动区域，这是设置滚动方向为水平方向出现的bug
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    
}

#pragma mark--setUpTopButtons

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    if (_isInitial==0) {
        //添加所有按钮
        [self setUpTopButtons];
        _isInitial=1;

    }
   
}

-(void)setUpTopButtons

{
    
    NSInteger count=self.childViewControllers.count;
    
    for (int i=0; i<count; i++) {
        
        UIViewController *VC= self.childViewControllers[i];
        
        UIButton *btn=[UIButton buttonWithType: UIButtonTypeCustom];
        //设置按钮数字标记
        btn.tag=i;
        //设置文字
        [btn setTitle:VC.title forState:UIControlStateNormal];
        //选中状态才会变红，而不是高亮状态
        [btn setTitle:VC.title forState:UIControlStateSelected];
        //设置文字颜色
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor redColor]forState:UIControlStateSelected];
        //设置frame
        CGFloat btnW=LSRScreenW/count;
        CGFloat btnH=44;
        CGFloat btnX=i*btnW;
        CGFloat btnY=0;
        [btn sizeToFit];
        btn.frame=CGRectMake(btnX, btnY, btnW, btnH);
        
        //别忘了添加到topView上面
        [self.topView addSubview:btn];
        
        [btn addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        
        //先使用懒加载进行初始化、否则数组是空的
        [self.buttonArray addObject:btn];
        
        if (i==0) {
            
            [self setButton:btn];
            
            UIView *redView=[[UIView alloc] init];
            
            redView.backgroundColor=[UIColor redColor];
            
            [btn.titleLabel sizeToFit];
            
            //            redView.xmg_centerX=btn.xmg_centerX;
            //            redView.xmg_height=h;
            //            redView.xmg_y=btn.xmg_height-h;
            //            redView.xmg_width=btn.titleLabel.xmg_width;
            
            
            [btn.titleLabel sizeToFit];
            redView.xmg_width = btn.titleLabel.xmg_width;
            CGFloat h = 2;
            redView.xmg_height = h;
            redView.xmg_y = btn.xmg_height - h;
            redView.xmg_centerX = btn.xmg_centerX;
            
            self.redView=redView;
            
            [self.topView addSubview:redView];
            //            [self buttonClick:<#(UIButton *)#>]
            
        };
        
        
        
    }
    //    NSLog(@"*****%@",self.buttonArray);
    
    
}
#pragma mark--buttonClick:
-(void)buttonClick:(UIButton *)btn
{
    
    
    self.collectionView.contentOffset=CGPointMake(btn.tag*LSRScreenW, 0);
    
    //    self.redView.contentOffset=CGPointMake(btn.tag*LSRScreenW, 0);
    
    
    [self setButton:btn];
    
    
}

-(void)setButton:(UIButton *)btn
{
    
    self.selctedBtn.selected=NO;
    
    btn.selected=YES;
    
    self.selctedBtn=btn;
    [UIView animateWithDuration:0.25 animations:^{
        self.redView.xmg_centerX=btn.xmg_centerX;
    }];
    
    
}

#pragma mark--setUpTopView
-(void)setUpTopView
{
    UIScrollView *topView=[[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, LSRScreenW, 44)];
    self.topView=topView;
    
    topView.backgroundColor=[UIColor colorWithWhite:1.0 alpha:0.75];
    //    topView.backgroundColor=[UIColor redColor];
    [self.view addSubview:topView];
    
    
    
}
#pragma mark--setUpBottomView
-(void)setUpBottomView
{
    //创建流水线布局
    UICollectionViewFlowLayout *layOut=[[UICollectionViewFlowLayout  alloc ] init];
    layOut.itemSize=CGSizeMake( LSRScreenW, LSRScreenH);
    layOut.minimumLineSpacing=0;
    layOut.minimumInteritemSpacing=0;
    //设置滚动方向，不在collectionView设置
    layOut.scrollDirection= UICollectionViewScrollDirectionHorizontal;
    //创建collectionView
    
    UICollectionView *collectionView=[[UICollectionView alloc] initWithFrame:self.view.bounds   collectionViewLayout:layOut] ;
    self.collectionView=collectionView;
    //设置数据源
    collectionView.dataSource=self;
    collectionView.delegate=self;
    collectionView.backgroundColor=[UIColor whiteColor];
    //设置滚动方向和分页
    collectionView.pagingEnabled=YES;
    
    
    [self.view addSubview:collectionView];
    
    [collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:ID];
    
}

#pragma mark--UICollectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.childViewControllers.count;
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    UICollectionViewCell *cell=[self.collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
    
    //    cell.backgroundColor = [UIColor colorWithRed:((float)arc4random_uniform(256) / 255.0) green:((float)arc4random_uniform(256) / 255.0) blue:((float)arc4random_uniform(256) / 255.0) alpha:1.0];
    //contentView所有的子控件都执行这个方法
    [cell.contentView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    //取出对应的控制器
    UIViewController *childVC=self.childViewControllers[indexPath.row];
    
    childVC.view.frame=CGRectMake(0, 0, LSRScreenW, LSRScreenH);
    //添加到contentView上
    [cell.contentView addSubview:childVC.view];
    
    
    return cell;
    
}
#pragma mark--UICollectionViewDelegate

//监听停止滚动的状态
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView

{
    //    self.topView.subviews,这样访问不好，还有两个子控件
    
    NSInteger i=scrollView.contentOffset.x/LSRScreenW;
    
    UIButton *button=self.buttonArray[i];
    
    [self setButton:button];
    
}







@end
